<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 id="log">Hello!</h1>
<div id="btu">
    <input id="cookie" type="hidden">
    <input id="getStream" value="获取Live" type="button" onclick="getStream()">
    <label for="deviceId">did:
        <input id="deviceId" type="text" value="69333341298">
    </label>
    <label for="streamUrl">推流地址:
        <input id="streamUrl" type="text">
    </label>
    <label for="streamKey">推流密钥：
        <input id="streamKey" type="text">
    </label>
</div>

<img th:src="'data:image/png;base64,'+ ${code.qrcode}">
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script th:inline="javascript">
    $(function (){
        checkQrConnect()
    })
    function checkQrConnect(){
        $.get('/dy/checkQrConnect/'+ [[${code.token}]],(res)=>{
            if( res.data.status == 1){
                $("#log")[0].innerText = '未扫码'
                checkQrConnect()
            }else if(res.data.status == 2){
                $("#log")[0].innerText = '已扫码'
                checkQrConnect()
            }else if(res.data.status == 3){
                $("#log")[0].innerText = '已确认'
                $("#btu")[0].style.display = 'block'
                $("#cookie")[0].value = res.cookie
            } else if(res.data.status == 5){
                location.reload()
            }
        })
    }
    function getStream(){
        let data = {
            cookie:$("#cookie").val(),
            deviceId:$("#deviceId").val()
        }
        $.post('/dy/getStream',data,(res)=>{
            if (res.status_code == 20003) {
                alert(res.data.prompts)
            } else if (res.status_code == 30003) {
                alert(res.data.prompts)
            }if(res.status_code == 0){
                const regex = /.+\//
                let streamUrl = regex.exec(res.data.stream_url.rtmp_push_url)[0]
                $("#streamUrl")[0].value = streamUrl
                let streamKey = res.data.stream_url.rtmp_push_url.replace(regex, '')
                $("#streamKey")[0].value = streamKey
                let liveUrl = res.data.share_url
            }
        })
    }
</script>
<style>
    #btu{
        display: none;
    }
</style>
</html>